package com.magicmovie.api.controller;

import com.google.common.collect.Maps;
import com.magicmovie.api.service.CustomerService;
import com.magicmovie.api.service.MovieService;
import com.magicmovie.api.service.OrderService;
import com.magicmovie.api.service.ViewHistoryService;
import com.magicmovie.entity.Customer;
import com.magicmovie.shiro.CurrentCustomerService;
import com.magicmovie.util.common.AjaxMsg;
import com.magicmovie.util.common.ValidUtil;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    @Autowired
    private CurrentCustomerService currentCustomerService;
    @Autowired
    private MovieService movieService;
    @Autowired
    private ViewHistoryService viewHistoryService;
    @Autowired
    private OrderService orderService;


    @RequestMapping(value = "/user/register",method = RequestMethod.POST)
    public AjaxMsg register(String username, String password){
        if(ValidUtil.isEmpty(username,password))
            return new AjaxMsg("500","必要参数不能为空！");
        Customer customer = customerService.findByUsername(username);
        if(customer != null)
            return new AjaxMsg("500","账号已存在，请重新输入！");
        customerService.register(username,password);
        return new AjaxMsg();
    }

    @RequestMapping(value = "/user/login",method = RequestMethod.POST)
    public AjaxMsg login(String username,String password){
        if(ValidUtil.isEmpty(username,password))
            return new AjaxMsg("500","必要参数不能为空！");
        Customer customer = customerService.findByUsername(username);
        if (customer == null || !customer.getPassword().equals(new Sha256Hash(password,customer.getSalt()).toHex()) )
            return new AjaxMsg(AjaxMsg.FAILURE, "账号或密码错误!");

        String token = customerService.createToken(customer);
        Map result = new HashMap();
        result.put("token", token);
        result.put("id", customer.getId());
        return new AjaxMsg(result);
    }

    @RequestMapping(value = "/user/logout",method = RequestMethod.POST)
    public AjaxMsg logout(){
        Customer customer = currentCustomerService.getCurrentCustomer();
        if(customer == null)
            return new AjaxMsg("500","当前不存在登陆账号!");
        customerService.logout(customer);
        return new AjaxMsg();
    }

    @RequestMapping(value = "/user/updatePassword",method = RequestMethod.POST)
    public AjaxMsg updatePassword(String oldPassword,String newPassword){
        if(ValidUtil.isEmpty(oldPassword,newPassword))
            return new AjaxMsg(AjaxMsg.FAILURE,"请求参数缺失！");
        if(oldPassword.equals(newPassword))
            return new AjaxMsg(AjaxMsg.FAILURE,"新旧密码不能相同！");

        Customer customer = currentCustomerService.getCurrentCustomer();
        if(!new Sha256Hash(oldPassword,customer.getSalt()).toHex().equals(customer.getPassword()))
            return new AjaxMsg(AjaxMsg.FAILURE,"旧密码错误！");

        customer.setPassword(new Sha256Hash(newPassword,customer.getSalt()).toHex());
        customerService.updateByPrimaryKeySelective(customer);
        return new AjaxMsg();
    }

    @RequestMapping(value = "/user/info",method = RequestMethod.GET)
    public AjaxMsg info(){
        Customer c = currentCustomerService.getCurrentCustomer();
        Map<String,Object> customer = Maps.newHashMap();
        customer.put("username",c.getUsername());
        customer.put("avatar",c.getAvatar());
        customer.put("balance",c.getBalance());

            Map<String, Object> purchasedMovie = movieService.findPurchasedMovie(c.getId(),1,3);
        Map<String, Object> viewMovie = viewHistoryService.findByCustomerId(c.getId(),1,3);

        Map<String,Object> result = Maps.newHashMap();
        result.put("customer",customer);
        result.put("purchasedMovie",purchasedMovie.get("result"));
        result.put("viewMovie",viewMovie.get("result"));
        return new AjaxMsg(result);
    }

    @RequestMapping(value = "/user/payHistory",method = RequestMethod.GET)
    public AjaxMsg payHistory(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,@RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        Customer c = currentCustomerService.getCurrentCustomer();
        if(c == null)
            return new AjaxMsg(AjaxMsg.FAILURE,"登陆凭证有问题，请重新登陆！");
        List<Map<String,Object>> map = orderService.findByCustomer(c.getId(),pageNum,pageSize);
        Map res = new HashMap();
        res.put("result",map);
        return new AjaxMsg(res);
    }
}
